home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / ranarg.zip / RANDARG.C next >
C/C++ Source or Header  |  1987-03-15  |  3KB  |  111 lines

  1. /* randarg -- select random argument                                        */
  2. /* copyright 1987  Michael M Rubenstein                                     */
  3.  
  4. #include <stdio.h>
  5.  
  6. #define LINESIZE        128
  7.  
  8. static int              from_file = FALSE;
  9. static int              do_command = FALSE;
  10.  
  11. extern char             *fgets();
  12. extern int              optind;
  13.  
  14. main(argc, argv)
  15.   int                   argc;
  16.   char                  *argv[];
  17. {
  18.   char                  line[LINESIZE + 1];
  19.   int                   i, n;
  20.   FILE                  *infile;
  21.  
  22.   while ((n = getopt(argc, argv, "cfh")) != EOF)
  23.     switch (n)
  24.     {
  25.       case 'f':     from_file = TRUE;
  26.                     break;
  27.  
  28.       case 'c':     do_command = TRUE;
  29.                     break;
  30.  
  31.       case 'h':
  32.       default:      help();
  33.                     exit(1);
  34.     }
  35.  
  36.   argc -= optind;
  37.   argv += optind;
  38.  
  39.   if (argc > 0)
  40.     if (from_file)
  41.     {
  42.       if ((infile = fopen(argv[0], "r")) == NULL)
  43.       {
  44.         fprintf(stderr, "randarg: Cannot open %s\n", argv[0]);
  45.         exit(1);
  46.       }
  47.       if (fgets(line, LINESIZE, infile) == NULL)
  48.         return;
  49.       n = atoi(line);
  50.       if (n > 0)
  51.       {
  52.         for (i = irand(n); i >= 0; --i)
  53.           if (fgets(line, LINESIZE, infile) == NULL)
  54.             break;
  55.         if (i < 0)
  56.           output(line);
  57.       }
  58.       fclose(infile);
  59.     }
  60.     else
  61.       output(argv[irand(argc)]);
  62. }
  63.  
  64. /* output line to stdout or do command, depending on do_command             */
  65. output(line)
  66.   char                  line[];
  67. {
  68.   int                   i;
  69.  
  70.   if ((i = strlen(line)) > 0 && line[i - 1] == '\n')
  71.     line[i - 1] = '\0';
  72.   if (line[0] != '\0')
  73.     if (do_command)
  74.       system(line);
  75.     else
  76.       printf("%s\n", line);
  77. }
  78.  
  79. /* generate random number                                                   */
  80. irand(max)
  81.  int                    max;
  82. {
  83.   unsigned              rslt;
  84.   long                  tloc;
  85.  
  86.   time(&tloc);
  87.   rslt = (tloc ^ (tloc >> 16)) | 1;
  88.   rslt *= 13;
  89.   return (rslt / 8) % max;
  90. }
  91.  
  92. /* show how to use                                                          */
  93. help()
  94. {
  95.   static char           *msg[] =
  96.   {
  97.     "syntax:  randarg [-c] <args>...\n",
  98.     "    or:  randarg [-c] -f <infile>\n\n",
  99.     "The first form randomly selects an argument and writes it to stdout\n",
  100.     "(if not -c) or executes it (if -c).\n\n",
  101.     "The second form randomly selects a line from <infile> and writes it\n",
  102.     "to stdout or executes it.  The first line of <infile> must contain\n",
  103.     "the number of lines in the remainder of the file\n",
  104.     NULL
  105.   };
  106.   char                  **p;
  107.  
  108.   for (p = msg; *p != NULL; ++p)
  109.     fputs(*p, stderr);
  110. }
  111.